package cn.lifeaid.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.lifeaid.db.SysUser;
import cn.lifeaid.db.SysUserExample;

@Service
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	SqlSession sqlSession;
	@Autowired
	private PasswordHelper passwordHelper;
	@Autowired
	private SysRoleService roleService;

	@Override
	public void changePassword(Long userId, String newPassword) {
		SysUser user = findOne(userId);
		user.setPassword(newPassword);
		passwordHelper.encryptPassword(user);
		updateUser(user);
	}

	@Override
	public SysUser findOne(Long userId) {
		return sqlSession.selectOne(
				"cn.lifeaid.db.SysUserMapper.selectByPrimaryKey", userId);
	}

	@Override
	public List<SysUser> findAll() {
		return findByExample(new SysUserExample());
	}

	@Override
	public SysUser findByUsername(String username) {
		SysUserExample example = new SysUserExample();
		SysUserExample.Criteria criteria = example.or();
		criteria.andUsernameEqualTo(username);
		example.setOrderByClause("create_time desc");
		example.setLimit(1);
		return findByExample(example).get(0);
	}

	@Override
	public List<Long> getRoleIds(SysUser user) {
		List<Long> roleIds = new ArrayList<Long>();
		if (null != user) {
			String[] roleIdStrs = user.getRoleIds().split(",");
			for (String roleIdStr : roleIdStrs) {
				if (StringUtils.isEmpty(roleIdStr)) {
					continue;
				}
				roleIds.add(Long.valueOf(roleIdStr));
			}
		}

		return roleIds;
	}

	@Override
	public Set<String> findRoles(String username) {
		SysUser user = findByUsername(username);
		if (user == null) {
			return Collections.emptySet();
		}
		return roleService.findRoles(getRoleIds(user).toArray(new Long[0]));
	}

	@Override
	public Set<String> findPermissions(String username) {
		SysUser user = findByUsername(username);
		if (user == null) {
			return Collections.emptySet();
		}
		return roleService.findPermissions(getRoleIds(user)
				.toArray(new Long[0]));
	}

	@Override
	public void createUser(SysUser user) {
		// 加密密码
		passwordHelper.encryptPassword(user);
		sqlSession.insert("cn.lifeaid.db.SysUserMapper.insert", user);
	}

	@Override
	public void updateUser(SysUser user) {
		sqlSession.update("cn.lifeaid.db.SysUserMapper.updateByPrimaryKey",
				user);
	}

	@Override
	public void deleteUser(Long userId) {
		sqlSession.delete("cn.lifeaid.db.SysUserMapper.deleteByPrimaryKey",
				userId);
	}

	@Override
	public List<SysUser> findByExample(SysUserExample example) {
		return sqlSession.selectOne(
				"cn.lifeaid.db.SysUserMapper.selectByExample", example);
	}

	@Override
	public String getCredentialsSalt(SysUser user) {
		return user.getUsername() + user.getSalt();
	}

}
